package com.h819.core.web;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

/**
 * @Title: ServletTest.java
 * @Description: TODO(添加描述)
 * @author H819
 * @date 2010-4-9
 * @version V1.0
 */
public class ServletDemo extends HttpServlet {

	ServletUtils su = new ServletUtils();

	/**
	 * Constructor of the object.
	 */
	public ServletDemo() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 获取请求参数

		String filePath = (String) request.getParameter("file");

		// 获取请求参数
		Enumeration<String> enu = request.getParameterNames();

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());

		out.println("getContextPath :" + su.getContextPath(request) + "</br>");

		out.println("getRequestPath :" + su.getRequestPath(request) + "</br>");

		while (enu.hasMoreElements()) {
			String para = enu.nextElement().toString();
			out.println(para + " :" + request.getParameter(para) + " <br/>");
		}

		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();

		
		//http://ml365.javaeye.com/blog/839173
		
		//运用forward方法只能重定向到同一个Web应用程序中的一个资源。而sendRedirect方法可以让你重定向到任何URL。
		
		// forward : 地址栏不发生变化
		// 是直接将请求转发到指定URL，所以该请求能够直接获得上一个请求的数据，也就是说采用请求转发，request对象始终存在，不会重新创建。
		// 而sendRedirect()会新建request对象，所以上一个request中的数据会丢失。
		request.getRequestDispatcher("/url").forward(request, response);
		request.getRequestDispatcher("/url").include(request, response);
		// sendRedirect:
		// 地址栏发生变化，跳转到指定的URL地址，产生一个新的request，所以要传递参数只有在url后加参数，如： url?id=1.
		response.sendRedirect("/url");
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);

	}

	/**
	 * Returns information about the servlet, such as author, version, and
	 * copyright.
	 * 
	 * @return String information about this servlet
	 */
	public String getServletInfo() {
		return "This is my default servlet created by Eclipse";
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
